package com.bytedance.common.plugin.framework.model;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.res.Resources;
import android.net.Uri;
import android.text.TextUtils;

import com.bytedance.common.plugin.baseface.BaseProxy;
import com.bytedance.common.plugin.framework.util.PluginUtil;
import com.bytedance.common.utility.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import inline.ZipUtil;

public final class Plugin {
    public static final class PluginInfo {
        public String proxyClass;
        public String pluginClass;
        public ResourceType resourceType;
        public ProcessType processType;
        public Boolean installResult;
        public String processNameSuffix;
        public List<String> processNameSuffixList;

        public PluginInfo(String proxyClass, String pluginClass, ResourceType resourceType,
                          ProcessType processType, String processNameSuffix,
                          List<String> processNameSuffixList, Boolean arg8) {
            this.proxyClass = proxyClass;
            this.pluginClass = pluginClass;
            this.resourceType = resourceType;
            this.processType = processType;
            this.processNameSuffix = processNameSuffix;
            this.processNameSuffixList = processNameSuffixList;
            this.installResult = arg8;
        }
    }

    public static final Map<String, PluginInfo> mPluginInfoMap = new HashMap();
    public final String packageName;
    public final String apkPath;
    public final String dexPath;
    public final String libPath;
    public final PackageInfo mPackageInfo;
    public final ApplicationInfo mApplicationInfo;
    public BaseProxy mBaseProxy;
    public Resources mResources;

    public Plugin(String packageName, String apkPath, String dexPath, String libPath, PackageInfo packageInfo, ApplicationInfo applicationInfo) {
        this.packageName = packageName;
        this.apkPath = apkPath;
        this.dexPath = dexPath;
        this.libPath = libPath;
        this.mPackageInfo = packageInfo;
        this.mApplicationInfo = applicationInfo;
    }

    public final String fileName() {
        return this.packageName + ".apk";
    }

    public static boolean isTargetProcess(Context context, String packageName) {
        boolean result;
        PluginInfo pluginInfo = Plugin.mPluginInfoMap.get(packageName);
        ProcessType processType = PluginUtil.getProcessType(context);
        if (pluginInfo == null || pluginInfo.processType.getTypeValue() > processType.getTypeValue()) {
            result = false;
        } else {
            String curProcessName = com.bytedance.common.plugin.a.getPluginDepend().getCurProcessName(context);
            if (!PluginUtil.isWorkProcess(curProcessName)) {
                result = true;
            } else if (curProcessName.endsWith(pluginInfo.processNameSuffix)) {
                result = true;
            } else if (pluginInfo.processNameSuffixList == null) {
                result = false;
            } else if (!pluginInfo.processNameSuffixList.isEmpty()) {
                Iterator<String> processNameSuffixIterator = pluginInfo.processNameSuffixList.iterator();
                while (processNameSuffixIterator.hasNext()) {
                    if (curProcessName.endsWith(processNameSuffixIterator.next())) {
                        return true;
                    }
                }
                return false;
            } else {
                return false;
            }
        }

        return result;
    }

    public static void setInstallResult(String packageName, Boolean installResult) {
        PluginInfo pluginInfo = Plugin.mPluginInfoMap.get(packageName);
        if (pluginInfo != null) {
            pluginInfo.installResult = installResult;
        }
    }

    public final boolean isPluginInstalled(Object[] args) {
        if ((TextUtils.isEmpty(this.apkPath)) || (TextUtils.isEmpty(this.dexPath)) || (TextUtils.isEmpty(this.libPath))) {
            return false;
        } else {
            File apkDir = new File(this.apkPath);
            File dexDir = new File(this.dexPath);
            File libDir = new File(this.libPath);
            if (!apkDir.exists()
                    || !apkDir.isDirectory()
                    || !dexDir.exists()
                    || !dexDir.isDirectory()
                    || !libDir.exists()
                    || !libDir.isDirectory()) {
                if (Logger.debug()) {
                    Logger.e("Plugin", "Plugin isPluginInstalled = false for dir null");
                }
                return false;
            } else if (this.mPackageInfo == null) {
                if (Logger.debug()) {
                    Logger.e("Plugin", "Plugin isPluginInstalled = false for packageInfo null");
                }
                return false;
            } else if (args.length > 0 && args[0] instanceof Integer && (Integer) args[0] > this.mPackageInfo.versionCode) {
                if (Logger.debug()) {
                    Logger.e("Plugin", "Plugin isPluginInstalled = false for version diff");
                }
                return false;
            }
            String fileName = this.fileName();
            File apkFile = new File(this.apkPath + "/" + fileName);
            File dexFile = new File(this.dexPath + "/" + fileName + ".classes1.zip");
            if (!apkFile.exists() || !ZipUtil.validZipFile(apkFile) || !dexFile.exists() || !ZipUtil.validZipFile(dexFile)) {
                if (Logger.debug()) {
                    Logger.e("Plugin", "Plugin isPluginInstalled = false for apk or zip vertify error");
                }
                return false;
            } else if (args.length > 1 && args[1] instanceof String) {
                String expectedMD5 = (String) args[1];
                String md5 = PluginUtil.fileToMD5(apkFile);
                if (md5 != null && (md5.equals(expectedMD5))) {
                    if (Logger.debug()) {
                        Logger.d("Plugin", "Plugin isPluginInstalled = true");
                    }
                    return true;
                } else {
                    if (Logger.debug()) {
                        Logger.e("Plugin", "Plugin isPluginInstalled = false for md5 check error");
                    }
                    return false;
                }
            } else {
                if (Logger.debug()) {
                    Logger.d("Plugin", "Plugin isPluginInstalled = true");
                }
                return true;
            }
        }
    }

    public final boolean a(Context arg4) {
        boolean v0_1;
        Object v0 = Plugin.mPluginInfoMap.get(this.packageName);
        if (v0 == null || ((PluginInfo) v0).resourceType != ResourceType.APP) {
            v0_1 = false;
        } else {
            v0 = Plugin.mPluginInfoMap.get(this.packageName);
            ProcessType v2 = PluginUtil.getProcessType(arg4);
            if (v0 != null && ((PluginInfo) v0).processType.getTypeValue() == v2.getTypeValue()) {
                v0_1 = true;
                return v0_1;
            }

            v0_1 = false;
        }

        return v0_1;
    }

    public static String tryCopyPluginApk(Context context, String originPath, String dstPath) throws Exception {
        InputStream inputStream;
        if (TextUtils.isEmpty(originPath) || context == null || TextUtils.isEmpty(dstPath)) {
            return null;
        } else {
            if (Logger.debug()) {
                Logger.d("Plugin", "Plugin tryCopyPluginApk start");
            }
            Uri uri = Uri.parse(originPath);
            if ("file".equals(uri.getScheme())) {
                String path = uri.getPath();
                if (path.startsWith("/android_assets/")) {
                    String fileName = path.substring(16);
                    if (Logger.debug()) {
                        Logger.d("Plugin", "Plugin tryCopyPluginApk doing");
                    }
                    inputStream = context.getAssets().open(fileName);
                } else {
                    inputStream = new FileInputStream(path);
                }

                if (inputStream == null) {
                    return null;
                }

                File output = new File(dstPath);
                output.getParentFile().mkdirs();
                if (output.exists()) {
                    PluginUtil.deleteFile(output);
                }

                PluginUtil.writeToFile(inputStream, output);
                if (!Logger.debug()) {
                    return dstPath;
                }

                Logger.d("Plugin", "Plugin tryCopyPluginApk done");
                return dstPath;
            } else {
                return null;
            }
        }
    }
}

